package com.huopaolan.yohuoshop.Mvp.View.Fragment;

import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.huopaolan.lib_core.Base.BaseFragment;
import com.huopaolan.yohuoshop.Adapter.CategoryAllAdapter;
import com.huopaolan.yohuoshop.Adapter.CategoryGoodsAdapter;
import com.huopaolan.yohuoshop.Bean.CategoryAllGoodsEntity;
import com.huopaolan.yohuoshop.Bean.CategoryGoodsEntity;
import com.huopaolan.yohuoshop.Di.Compont.DaggerCategoryFragmentCompont;
import com.huopaolan.yohuoshop.Di.Modules.CategoryFragmentModules;
import com.huopaolan.yohuoshop.Mvp.Contract.CategoryFragmentContract;
import com.huopaolan.yohuoshop.Mvp.Presenter.CategoryFragmentPresenter;
import com.huopaolan.yohuoshop.R;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;

import java.util.ArrayList;
import java.util.List;

public class CategoryFragment extends BaseFragment<CategoryFragmentPresenter> implements CategoryFragmentContract.ICategoryView, BaseRefreshListener {
    private RecyclerView category_recycler, all_recycler;
    private PullToRefreshLayout pullToRefreshLayout;
    private View category_line_head;
    private TextView category_head_name;
    private CategoryGoodsAdapter goods_adapter;
    private CategoryAllAdapter allAdapter;
    public static List<CategoryAllGoodsEntity.Values> valuesList = new ArrayList<>();

    @Override
    public View createView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_category, null);
    }

    @Override
    public void initView(View view) {
        pullToRefreshLayout = view.findViewById(R.id.category_goods_pull);
        pullToRefreshLayout.setRefreshListener(this);
        //右边RecyclerView商品
        category_recycler = view.findViewById(R.id.category_recycler_goods);
        all_recycler = view.findViewById(R.id.category_all_recycler);
        //商品头布局
        category_line_head = View.inflate(getActivity(), R.layout.category_head_layout, null);
        category_head_name = category_line_head.findViewById(R.id.all_goods);
        //选项配置
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        all_recycler.setLayoutManager(layoutManager);
        allAdapter = new CategoryAllAdapter(R.layout.category_all_layout);
        all_recycler.setAdapter(allAdapter);
        //商品配置
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3, GridLayoutManager.VERTICAL, false);
        category_recycler.setLayoutManager(manager);
        goods_adapter = new CategoryGoodsAdapter(R.layout.category_goods_layout);
        //添加头布局
        goods_adapter.addHeaderView(category_line_head, 0);
        category_recycler.setAdapter(goods_adapter);
        allAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                goods_adapter.getData().clear();
                p.requestClassify();
                goods_adapter.notifyDataSetChanged();
                Toast.makeText(getActivity(), "" + (position + 1), Toast.LENGTH_SHORT).show();
                for (int i = 0; i < allAdapter.getData().size(); i++) {
                    allAdapter.getData().get(i).flag = false;
                }
                allAdapter.getData().get(position).flag = true;
                allAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onStart() {
        Window window = getActivity().getWindow();
        window.setStatusBarColor(Color.BLACK);
        super.onStart();
    }

    @Override
    public void initData() {
        p.requestClassify();
        p.requestAll();
    }

    @Override
    public void setUpDagger() {
        DaggerCategoryFragmentCompont.builder()
                .categoryFragmentModules(new CategoryFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void goodsSuccess(CategoryGoodsEntity entity) {
        if (entity == null) {
            return;
        }
        Log.d("HPL", "CategoryGoodsEntity" + entity.values.size());
        goods_adapter.addData(entity.values);
    }

    @Override
    public void goodsAllSuccess(CategoryAllGoodsEntity entity) {
        if (entity == null) {
            return;
        }
        valuesList.addAll(entity.values);
        Log.d("goodsAllSuccess", "" + entity.values.size());
        Toast.makeText(getActivity(), "" + entity.values.get(0).category_name, Toast.LENGTH_SHORT).show();
        allAdapter.addData(entity.values);
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void refresh() {
        goods_adapter.getData().clear();
        p.requestClassify();
        pullToRefreshLayout.finishRefresh();
    }

    @Override
    public void loadMore() {
        pullToRefreshLayout.finishLoadMore();
    }
}
